package com.demo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

import com.hcvsa.MyHCVSA;
import com.until.BusProvider;
import com.view.MyTPSurfaceView;
import com.view.MyVideoRel2;

import butterknife.OnClick;
import fun.flyee.android.pengshan.acts.BaseActivity;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.pengshan.social.R;
import hcvs.camera.ICameraSurface;
import hcvs.hcvsa.HCVSA;
import hcvs.videorender.MySurfaceView;

public class MonitorActivity extends BaseActivity implements OnClickListener, MyVideoRel2.MyVideoPlayLinstener {

    private Context context;
    protected MyVideoRel2 myVideoRel;

    private int channelid;    //打开视频的ID号

    @Override
    protected int layoutId() {
        return R.layout.activity_monitor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Systems.hideSystemStatusBar(this);
        Systems.hideSystemNavBar(this);
//        setContentView(R.layout.activity_monitor);
        BusProvider.getInstance().register(this);
        configInit();
        findView();
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        BusProvider.getInstance().unregister(this);
        //关闭视频
        closeChannel();
        super.onDestroy();
    }

    @OnClick(R.id.back_btn)
    public void onBack() {
        this.finish();
    }

    private void configInit() {
        context = this;
    }

    private void findView() {
        myVideoRel = findViewById(R.id.myVideoRel);
        myVideoRel.setMyVideoPlayLinstener(this);
    }

    private void init() {
        //待打开视频的通道ID号
        channelid = getIntent().getIntExtra("channelid", 0);

        openChannel();

        //SurfaceView不走surfaceCreated回调，更新一下UI即可。
        myVideoRel.post(new Runnable() {
            @Override
            public void run() {
                myVideoRel.updateUI();
            }
        });

        if (myVideoRel.getMyTPSurfaceViewList().size() > 0) {
            MyTPSurfaceView myTPSurfaceView = (MyTPSurfaceView) myVideoRel.getMyTPSurfaceViewList().get(0);
            if (myTPSurfaceView != null) {
                //窗口大小改变事件
                myTPSurfaceView.setOnVideoSizeChanged(new MySurfaceView.OnVideoSizeChanged() {
                    @Override
                    public void onVideoSizeChanged(int width, int height) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                displayTypeChanged(width, height);
                            }
                        });
                    }
                });
            }
        }
    }

    //打开视频监控
    private void openChannel() {
        //设置静音
        HCVSA.SetAudioPlayMute(1);
        //打开通道
        MyHCVSA.getInstance().getMonitorMoudle().RequestCMSOpenChannel(0, 0, channelid, HCVSA.TB_STREAM_TYPE_MAIN, false);
        //decodeTypeChanged();
    }

    //关闭视频监控
    private void closeChannel() {
        MyHCVSA.getInstance().getMonitorMoudle().RequestCloseChannel(channelid);

        if (myVideoRel != null) {//释放资源
            try {
                myVideoRel.release();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }

    @Override
    public void OnNewSurfaceAdded(SurfaceView surfaceView) {

    }

    @Override
    public void OnOpenMyCamera(ICameraSurface cameraSurface) {

    }

    @Override
    public void OnViewClick(SurfaceView surfaceView) {

    }

    //窗口宽度高度改变时调用
    private void displayTypeChanged(int width, int height) {

        Display display = getWindowManager().getDefaultDisplay();
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) myVideoRel.getLayoutParams();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        if (width == 0 || height == 0) return;

        if (width < screenWidth && height < screenHeight) {//宽度和高度均小于屏幕尺寸

            if (screenWidth - width <= screenHeight - height) {//如果宽度差比高度差小，则取屏幕宽度为准。
                lParams.width = screenWidth;
                lParams.height = screenWidth * height / width;
            } else {
                lParams.width = width * screenHeight / height;
                lParams.height = screenHeight;
            }

        } else if (width >= screenWidth && height >= screenHeight) {//宽度或高度超出
            if (width - screenWidth <= height - screenHeight) {
                lParams.width = width * screenHeight / height;
                lParams.height = screenHeight;
            } else {
                lParams.width = screenWidth;
                lParams.height = screenWidth * height / width;
            }
        } else if (width >= screenWidth && height < screenHeight) {//宽度大于屏幕宽度，则取宽度
            lParams.width = screenWidth;
            lParams.height = screenWidth * height / width;
        } else if (width < screenWidth && height >= screenHeight) {//高度大于屏幕高度，则取高度
            lParams.width = width * screenHeight / height;
            lParams.height = screenHeight;
        } else {
            lParams.width = screenWidth;
            lParams.height = screenHeight;
        }
        lParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        myVideoRel.setLayoutParams(lParams);
        myVideoRel.post(new Runnable() {
            @Override
            public void run() {
                myVideoRel.updateUI();
            }
        });

    }
}
